package com.windfone.local;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.windfone.model.MemberDetail;
import com.windfone.service.MemberService;

public class ImportMember {

	public static boolean isDigit(char c){
		String standarstr = "1234567890.";
		boolean rtv = false;
		for(int i=0;i<standarstr.length();i++){
			if (standarstr.charAt(i) == c){
				rtv = true;
				break;
			}
		}
		return rtv;
	}
	public static String[] fiterString(String args){
		List<String> rtv = new ArrayList<String>();
		for(int j=0;j<args.length();j++){
			char beginChar = args.charAt(j);
			if ( isDigit(beginChar)){
				StringBuffer charbuf = new StringBuffer();
				charbuf.append( beginChar );
				for(int k=j+1;k<args.length();k++){
					char secondchar = args.charAt(k);
					if ( isDigit(secondchar ) ){	
						charbuf.append( secondchar );
						continue;
					} else {
						j=k-1;
						break;
					}
				}
				rtv.add( charbuf.toString() );
			}
 
		}
	 
		String[] rtvs =new String[rtv.size()];
		for(int i=0;i<rtv.size();i++){
			rtvs[i] = rtv.get(i);
		}
		return rtvs;
	}
	
	public static void main(String[] args) throws Exception {
		List<Hashtable> hs = new ArrayList<Hashtable>();
		try {
//			Workbook book = Workbook.getWorkbook(new File("d:/customer_kangle_new.xls"));
			Workbook book = Workbook.getWorkbook(new File("d:/customer_nanyou_new.xls"));
			Sheet sheet = book.getSheet(0);
//			for(int i=2;i< sheet.getRows();i++){
			for(int i=0;i< sheet.getRows();i++){
				Hashtable t = new Hashtable ();
				Cell cell = sheet.getCell(0,i);
				String result = cell.getContents();
				if ( result==null || result.equals("")) {
					break;
				}
				Member m = new Member();
				m.setExcelRow(i);
				m.setCardId( sheet.getCell(2,i).getContents() );
				m.setName(   sheet.getCell(1,i).getContents() );
				if (m.getCardId().equals("")){
					m.setPassword(  "12345678"  );
				}else {
					m.setPassword(  m.getCardId()  );
				}
				m.setStatus(1);
				m.setLevel(1);
				t.put("member", m);
				List<Orderhistory> goods = new ArrayList<Orderhistory>();
				if ( ! sheet.getCell(3,i).getContents().equals("")){
					Orderhistory mobile = new Orderhistory();
					mobile.setBuydate(   sheet.getCell(0,i).getContents()  );
					mobile.setExcelRow(i);
					mobile.setPrice(  sheet.getCell(5,i).getContents() );
					mobile.setProductName( sheet.getCell(3,i).getContents() );
					mobile.setSerialNumber( sheet.getCell(4,i).getContents() );
					goods.add(mobile);
				} 
				if ( !sheet.getCell(6,i).getContents().equals("")){
					Orderhistory fitting = new Orderhistory();
					fitting.setExcelRow(i);
					String prs =  sheet.getCell(7,i).getContents(); 
					if ( prs!=null && !prs.equals("")){
						String[] priceArray = fiterString(prs);
						int total = 0;
						for(int j=0;j<priceArray.length;j++){
							total = total + Integer.parseInt( priceArray[j]);
						}
						fitting.setPrice( ""+total );
						fitting.setRemark( prs );
					}
 
					fitting.setProductName( sheet.getCell(6,i).getContents() );
					fitting.setBuydate(   sheet.getCell(0,i).getContents()  );
					goods.add(fitting);
				} 
				t.put("goods", goods);
				hs.add(t);
 
			}
 
			book.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		
		List<Hashtable> hs2 = new ArrayList<Hashtable>();
		boolean addFlag = false;
		for(int i=0;i<hs.size();i++){
			Member outer = (Member)hs.get(i).get("member");
			for(int j=i+1;j<hs.size();j++){
				Member inner = (Member)hs.get(j).get("member");
				String cardId1 = outer.getCardId();
				String cardId2 =  inner .getCardId();
				if ( !cardId2.equals("") && ! cardId1.equals("")){
					if ( cardId1.equals( cardId2 )){
						List goods1 = (List)hs.get(i).get("goods"); 
						List goods2 = (List)hs.get(j).get("goods"); 
						for(int k=0;k<goods2.size();k++){
							goods1.add( goods2.get(k) );
						}
						hs.get(i).remove("goods");
						hs.get(i).put("goods", goods1);
						inner.setDealFlag(true);
					}
				}
			}
			if ( outer.isDealFlag()==false){
				hs2.add( hs.get(i));
			}
		}
		ApplicationContext  ctx = new ClassPathXmlApplicationContext ("applicationContext.xml");
//		ApplicationContext  ctx = new FileSystemXmlApplicationContext("D:\\development\\workspace3.6\\windfone\\windfone-web\\src\\main\\webapp\\WEB-INF\\applicationContext.xml");
		MemberService memberService= (MemberService) ctx.getBean("memberService");
		for( int i=0;i<hs2.size();i++){
			Hashtable hashtable = hs2.get(i);
			Member bean = (Member)hashtable.get("member");
			com.windfone.model.Member bean2= memberService.checkMemberByCardId(bean);
			long key = -1l;
			if ( bean2==null){
				key = memberService.addMemberGetReturnKey(bean);
				MemberDetail detailBean = new MemberDetail();
				detailBean.setMemberId((int)key);
				detailBean.setMobileNumber(bean.getCardId());
				detailBean.setSex(-1);
				memberService.addMemberDetail(detailBean);
			}	else {
				key = bean2.getId();
			}
			List<Orderhistory> goods  = (List<Orderhistory>)hashtable.get("goods");
			for(int j=0;j<goods.size();j++){
				Orderhistory history = goods.get(j);
				history.setUserId((int)key);
				memberService.addOrderHistory(history);
				com.windfone.model.Member member  = memberService.getMemberByKey((int)key);
				System.out.println( member.getPassword() );
				int p1 = member.getPoints();
				int p2 = member.getPointsb();
				int p3 = 0;
				String price = history.getPrice();
				if ( price !=null && !price.equals("")){
					p3 = (int)Double.parseDouble(price);
				}
				int p4 = p1+ p3;
				int p5 = p2 + p3;
				member.setPoints( p4  );
				member.setPointsb( p5 );
				memberService.updateMember(member);
			}
			
		} 
	}
	
}
